﻿using System;
using System.Collections.Generic;
using System.Windows.Media;
using System.Windows.Media.Media3D;
using HelixToolkit.Wpf;

namespace GameBoard
{
    internal class TapeMeasurerControl
    {
        public enum DrawModeTapeMeasurer
        {
            StartOnly,
            End
        }

        public DrawModeTapeMeasurer TapeMeasurerDrawMode { get; set; }
        private SphereVisualFactory _startPointFactory;

        public SphereVisualFactory StartPointFactory
        {
            get
            {
                return _startPointFactory;
                ;
            }
        }

        private SphereVisualFactory _endPointFactory;

        public SphereVisualFactory EndPointFactory
        {
            get
            {
                return _endPointFactory;
                ;
            }
        }

        private InfoTextVisualFactory _textVisual;

        public InfoTextVisualFactory TextVisualFactory
        {
            get
            {
                return _textVisual;
                ;
            }
        }

        private TubeVisualFactory _distanceIndicator;

        public TubeVisualFactory DistanceIndicatorFactory
        {
            get
            {
                return _distanceIndicator;
                ;
            }
        }

        private Point3D _startPoint;
        private Point3D _endPoint;

        internal void HandleHit(Point3D pointHit)
        {
            if (_startPointFactory != null && _endPointFactory != null)
            {
                CleanupTapeMeasurerVisuals();
            }
            if (_startPointFactory == null)
            {
                var startData = new SphereVisualData
                {
                    Center = pointHit,
                    MaterialColor = Colors.Black,
                    Radius = 2,
                };
                _startPointFactory = new SphereVisualFactory(startData);
                _startPoint = pointHit;
                TapeMeasurerDrawMode = DrawModeTapeMeasurer.StartOnly;
            }
            else if (_endPointFactory == null)
            {
                var endData = new SphereVisualData
                {
                    Center = pointHit,
                    MaterialColor = Colors.Black,
                    Radius = 2,
                };
                _endPoint = pointHit;
                _endPointFactory = new SphereVisualFactory(endData);
                //_gameBoardVisual.AddVisual(_endPoint);
                double distance = (_startPoint - _endPoint).Length;
                distance = Math.Round(distance, 0);
                var textPos = _endPoint;
                textPos.Z += 30;
                var textVisualData = new InfoTextVisualData
                {
                    Text = distance + " units.",
                    Position = textPos,
                    Background = new SolidColorBrush(Colors.Black),
                    FontSize = 28,
                    HeightChange = 0,
                    Foreground = new SolidColorBrush(Colors.LightGray)
                };
                _textVisual = new InfoTextVisualFactory(textVisualData);


                var distanceIndicatorData = new TubeVisualData
                {
                    Path = new Point3DCollection(new List<Point3D> {_startPoint, _endPoint}),
                    MaterialColor = Colors.LightGray,
                    Diameter = 1.4,
                };
                _distanceIndicator = new TubeVisualFactory(distanceIndicatorData);
                TapeMeasurerDrawMode = DrawModeTapeMeasurer.End;
            }
        }

        internal void CleanupTapeMeasurerVisuals()
        {
            TapeMeasurerDrawMode = DrawModeTapeMeasurer.End;
            if(_startPointFactory!=null)
                VisualsViewModel.Instance.RemoveVisuals(_startPointFactory.FactoryVisualID);
            if (_endPointFactory != null)
                VisualsViewModel.Instance.RemoveVisuals(_endPointFactory.FactoryVisualID);
            if (_textVisual != null)
                VisualsViewModel.Instance.RemoveVisuals(_textVisual.FactoryVisualID);
            if (_distanceIndicator != null)
                VisualsViewModel.Instance.RemoveVisuals(_distanceIndicator.FactoryVisualID);
            _startPointFactory = null;
            _endPointFactory = null;
            _textVisual = null;
            _distanceIndicator = null;
        }
    }
}